#include "agent.h"
#include "edge.h"
#include "node.h"
#include "math.h"
#include "graphwidget.h"
#include "traffic_sim.h"
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include <math.h>
#include <iostream>
#include "graphwidget.h"
#include <QGraphicsItemAnimation>
#include <QtGui>

class Node;
using namespace Traffic_Sim;

using namespace std;

Agent::Agent(GraphWidget *graphWidget, Traffic_Sim::Traffic_Vehicle * vehicle) : graphwidget(graphWidget), vehicle(vehicle)
{
  setFlag(ItemIsMovable);
  setZValue(0);
  flag=1;
  
}
Agent::Agent(){}
Agent::Agent(Node * node1)
{
  node=node1;
}

void Agent::setAgent(Traffic_Agent_Impl* agent_impl1){
  Traffic_Vehicle * vehicle1 = static_cast<Traffic_Vehicle *>(agent_impl1);
  vehicle =  vehicle1;
  
}

void Agent::setGraphWidget(GraphWidget * graphwidget1){
  graphwidget=graphwidget1;
}

QRectF Agent::boundingRect() const
{
  return QRectF(0,0 ,4000,4000);
}

void Agent::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
  QRectF rect = QRectF(0,0 ,60,60 );
  QString str = QString::number ( vehicle->get_id() );
  
  Traffic_Node * node;
  Traffic_Node * node1;
  
  if (vehicle->get_agent().in_node)
    {
      node = static_cast<Traffic_Node *>(vehicle->get_agent().location);
    }else{
      Traffic_Arc * arc = static_cast<Traffic_Arc *>(vehicle->get_agent().location);
      

if(vehicle->previous_node==arc->tgt_node){
	node = static_cast<Traffic_Node *>(arc->tgt_node);
	node1 = static_cast<Traffic_Node *>(arc->src_node);
}
if(vehicle->previous_node==arc->src_node){
	node = static_cast<Traffic_Node *>(arc->src_node);
        node1 = static_cast<Traffic_Node *>(arc->tgt_node);
}

      int move;
      move = arc->get_info().get_delay_time();
      
      if( vehicle->get_last_event()==Node_Departure){
	
	
	painter->drawRect(10, 10, 30, 30);
	painter->setBrush(Qt::black);
	painter->drawEllipse(0, 0, 10, 10);
	painter->drawEllipse(0, 40, 10, 10);
	painter->drawEllipse(40, 0, 10, 10);
	painter->drawEllipse(40, 40, 10, 10);
	painter->drawText(0,0,50,50,Qt::AlignCenter,str);
	QTimeLine *timer = new QTimeLine(5000);
	timer->setFrameRange(9, 10);
	QGraphicsItemAnimation *animation = new QGraphicsItemAnimation;
	animation->setItem(this);
	animation->setTimeLine(timer);
	setPos(node->get_info().x,node->get_info().y);
	qreal e=node->get_info().x;
	qreal f=node->get_info().y;
	qreal w=node1->get_info().x;
	qreal z=node1->get_info().y;
	qreal y,step,x;
	qreal dx=(w-e);
	qreal dy=(z-f);
	animation->setPosAt(0, QPointF(e,f));
	step= abs(w-e)/move;
	x=e; 
	// cout<<"Agente "<< vehicle->get_id()<<" Inicial("<<e<<","<<f<<") Final ("<<w<<","<<z<<") Paso"<<step<<endl;
	for (int i = 0; i <= move; i++){
	  y=((dy/dx)*(x-e))+f;
	  animation->setPosAt(i/ move, QPointF(x,y));
	  //	cout<<"("<<x<<","<<y<<")";
	  if((e>0&&w>0)&&e>w){
	    x-=step;
	  }
	  if((e>0&&w>0)&&e<w){
	    x+=step;
	  }
	  if((e<0&&w<0)&&abs(e)>abs(w)){
	    x+=step;
	  }
	  if((e<0&&w<0)&&abs(e)<abs(w)){
	    x-=step;
	  }
	  if(e<0&&w>0){
	    x+=step;
	  }
	  if(e>0&&w<0){
	    x-=step;
	  }
	}
	timer->start();}
    }
  
  
  if( vehicle->get_last_event()==Agent_Creation){
    painter->setBrush(Qt::blue);
    setPos(node->get_info().x,node->get_info().y);
    painter->drawRect(10, 10, 30, 30);
    painter->setBrush(Qt::black);
    painter->drawEllipse(0, 0, 10, 10);
    painter->drawEllipse(0, 40, 10, 10);
    painter->drawEllipse(40, 0, 10, 10);
    painter->drawEllipse(40, 40, 10, 10);
    painter->drawText(0,0,50,50,Qt::AlignCenter,str);
  }
  if( vehicle->get_last_event()==Node_Arrival){
    painter->setBrush(Qt::green);
    setPos(node->get_info().x,node->get_info().y);
    painter->drawRect(10, 10, 30, 30);
    painter->setBrush(Qt::black);
    painter->drawEllipse(0, 0, 10, 10);
    painter->drawEllipse(0, 40, 10, 10);
    painter->drawEllipse(40, 0, 10, 10);
    painter->drawEllipse(40, 40, 10, 10);
    painter->drawText(0,0,50,50,Qt::AlignCenter,str);
  }
  if(vehicle->get_last_event()==Semaphore_Check){
    painter->setBrush(Qt::red);
    setPos(node->get_info().x,node->get_info().y);
    painter->drawRect(10, 10, 20, 20);
    painter->setBrush(Qt::black);
    painter->drawEllipse(0, 0, 10, 10);
    painter->drawEllipse(0, 40, 10, 10);
    painter->drawEllipse(40, 0, 10, 10);
    painter->drawEllipse(40, 40, 10, 10);
    painter->drawText(0,0,50,50,Qt::AlignCenter,str);
  }
  if(vehicle->get_last_event()==Agent_Destruction){
    return;
  }
  
}
